var txt;
var userName = $('#userName').val();
var passWord = $('#passWord').val();
$.ajax({
    url: Feng.ctxPath + "/outpatientManagement/getLists",
    type: "post",
    data: {"userName": userName},
    success: function (data) {
        for (var i = 0; i < data.length; i++) {
            txt += '<option style="color: #666666;" value="' + data[i].simplename + '">' + data[i].simplename + '</option>';
        }
        document.getElementById('outpatientNamed').innerHTML = txt;
        var seleteooutpatientName = $('#outpatientNameValue').val();
        if (seleteooutpatientName !== "") {
            $("#outpatientNamed option[value='" + seleteooutpatientName + "']").attr("selected", "selected");
        }
    },
    error: function (data) {
        alert('权限被限制，请联系管理员!');
        window.location.href = Feng.ctxPath + "/login";
    }
});

$("#outpatientNamed").change(function () {
    var outpatientName = $('#outpatientNamed').val();
    $.ajax({
        url: Feng.ctxPath + "/login",
        type: "post",
        data: {
            "username": userName,
            "outpatientName": outpatientName,
            "password": passWord
        },
        success: function (data) {
            window.location.href = Feng.ctxPath + "/index" ;
        },
        error: function (data) {
            alert('权限被限制，请联系管理员!');
            window.location.href = Feng.ctxPath + "/login";
        }
    });
});

var pageSize = 10;
//  市场
function marketSearch() {
    var reach = $("#marketInputPublic").val().trim();
    if (reach === "" || reach === undefined || reach === null) {
        $("#marketSearchContent").hide().html("");
    } else {
        $.ajax({
            url: Feng.ctxPath + "/storeRecords/getReservationsByReach",
            type: "post",
            data: {
                reach: reach,
                pageStart: $("#pageStart").val(),
                pageSize: pageSize
            },
            success: function (info) {
                var data = info.customerInfoList;
                var marketSearchContent = '';   // 总容器
                var marketContent = '';     // 内容容器
                var marketJudge = '';       // 判断是否市场
                var marketBackground = '';  // 背景色
                var marketDirect = '';      // 判断是否直接上门
                //  判断是否查询到患者
                if(data.length > 0){
                    for(var i = 0 ; i < data.length ; i ++){
                        //  判断是否市场
                        if(data[i].status === "到店"){
                            marketJudge = "marketSearchShop";
                            marketBackground = 'background: #5698c8;';
                        }else if(data[i].status === "预约"){
                            marketJudge = "marketSearchMake";
                            marketBackground = 'background: #32b16c';
                        }
                        //  判断是否直接上门  拼接内容
                        if(data[i].department === "直接上门"){
                            marketDirect = '<div class="marketJudge"><span class="marketCustomerName">' + data[i].customerName + '&nbsp;&nbsp;</span><span class="marketStatus" style="'+marketBackground+'">' + data[i].status + '</span></div>';
                        }else {
                            marketDirect = '<div class="marketJudge"><span class="marketCustomerName">' + data[i].customerName + '&nbsp;&nbsp;</span><span class="marketStatus" style="'+marketBackground+'">' + data[i].status + '</span><span class="marketStatus" style="background: #ff7e00;margin-left: 7px">S</span></div>';
                        }
                        marketContent += '<div class="'+marketJudge+'" id="' + data[i].id + '"><div><img src="' + data[i].customerSexHead + '" alt="" width="50px;"></div><div>'+marketDirect+'' +
                            '<div><span>' + data[i].customerPhone + '&nbsp;</span><span>/&nbsp;' + data[i].customerPhone2 + '</span></div>' +
                            '<div><span>' + data[i].arrivalStore + '&nbsp;</span><span>/&nbsp;' + data[i].customerAge + '岁&nbsp;</span><span>/&nbsp;' + data[i].sex + '</span></div>' +
                            '</div></div>'
                    }
                    //  整合 html  添加到页面
                    marketSearchContent = '<div style="font-size: 14px;padding: 4px 10px;border-bottom: 1px solid #dddddd;">找到' + info.count + '个患者</div>' +
                        '<div class="marketContent" style="height: 323px;overflow: auto">' + marketContent + '' +
                        '<div style="text-align: center;padding:5px 10px;border-top: 1px solid #cccccc;font-size: 14px;"><a href="javascript:;" id="marketSearchNum">点击加载更多</a></div></div>';
                    $("#marketSearchContent").html(marketSearchContent).show();
                }else {
                    marketSearchContent = '<div style="font-size: 14px;padding: 4px 10px;border-bottom: 1px solid #dddddd;">未搜索到患者</div>';
                    $("#marketSearchContent").html(marketSearchContent);
                }
            },
            error: function (data) {
                alert('没有权限，请联系管理员!');
            }
        });
    }
}
//  TC or 医生 or 结账
function doctorTcSearch(objId,objContent) {
    var reach = $('#'+objId).val();
    if (reach === "" || reach === undefined || reach === null) {
        $("#"+objContent).hide().html("");
    } else {
        $.ajax({
            url: Feng.ctxPath + "/customer/getCustomerByReach",
            type: "post",
            data: {
                reach: reach,
                pageStart: $("#pageStart").val(),
                pageSize: pageSize
            },
            success: function (info) {
                var data = info.customerInfoList;
                var doctorSearchContent = '';   // 总容器
                var doctorContent = '';     // 内容容器
                var doctorChange = '';      // 变动容器
                var judgeId = '';           // 判断 TC or 结账 or 医生
                //  判断是否查询到患者
                if (data.length > 0) {
                    for(var i = 0 ; i < data.length ; i ++){
                        //  判断是否欠费
                        if(data[i].accountArrears > 0){
                            //  判断是否会员
                            if(data[i].orthodonticsStatus !== '0.00'){
                                doctorChange = '<div class="doctorJudge"><span class="marketCustomerName">' + data[i].customerName + '</span>&nbsp;/&nbsp;<span>' + data[i].customerAge + '岁</span>&nbsp;/&nbsp;<span>' + data[i].sex + '</span>&nbsp;<span class="marketStatus" style="background: red;border-radius: 50%;">欠</span>&nbsp;<span style="display: inline-block;"><img src="https://pynnn.oss-cn-hangzhou.aliyuncs.com/ychh001.png" alt="..." width="18px;"></span></div>';
                            }else {
                                doctorChange = '<div class="doctorJudge"><span class="marketCustomerName">' + data[i].customerName + '</span>&nbsp;/&nbsp;<span>' + data[i].customerAge + '岁</span>&nbsp;/&nbsp;<span>' + data[i].sex + '</span>&nbsp;<span class="marketStatus" style="background: red;border-radius: 50%;">欠</span></div>';
                            }
                        }else {
                            //  判断是否会员
                            if(data[i].orthodonticsStatus !== '0.00'){
                                doctorChange = '<div class="doctorJudge"><span class="marketCustomerName">' + data[i].customerName + '</span>&nbsp;/&nbsp;<span>' + data[i].customerAge + '岁</span>&nbsp;/&nbsp;<span>' + data[i].sex + '</span>&nbsp;<span style="display: inline-block;"><img src="https://pynnn.oss-cn-hangzhou.aliyuncs.com/ychh001.png" alt="..." width="18px;"></span></div>';
                            }else {
                                doctorChange = '<div class="doctorJudge"><span class="marketCustomerName">' + data[i].customerName + '</span>&nbsp;/&nbsp;<span>' + data[i].customerAge + '岁</span>&nbsp;/&nbsp;<span>' + data[i].sex + '</span>&nbsp;</div>';
                            }
                        }
                        //  拼接内容
                        doctorContent += '<div class="marketSearchShop" id="' + data[i].id + '"><div><img src="' + data[i].customerSexHead + '" alt="" width="50px;"></div><div>' + doctorChange + '' +
                            '<div><span>' + data[i].customerPhone + '&nbsp;</span><span>/&nbsp;' + data[i].customerPhone2 + '</span></div>' +
                            '<div><span>' + data[i].medicalRecordNumber + '</span><span>&nbsp;/&nbsp;' + data[i].arrivalStore + '</span></div>' +
                            '</div></div>';
                    }
                    //  判断 TC or 结账 or 医生
                    if(objId === 'doctorTcInputPublic'){ // TC
                        judgeId = 'doctorTcSearchNum'
                    }else if(objId === 'doctorTcInputPublics'){ // 结账
                        judgeId = 'doctorTcSearchNums'
                    }else if(objId === 'doctorInputPublic'){ // 医生
                        judgeId = 'doctorSearchNum'
                    }
                    //  整合 html  添加到页面
                    doctorSearchContent = '<div style="font-size: 14px;padding: 4px 10px;border-bottom: 1px solid #dddddd;">找到' + info.count + '个患者</div>' +
                        '<div style="height: 323px;overflow: auto">' + doctorContent + '' +
                        '<div style="text-align: center;padding:5px 10px;border-top: 1px solid #cccccc;font-size: 14px;"><a href="javascript:;" id="'+judgeId+'">点击加载更多</a></div></div>';
                    $("#"+objContent).html(doctorSearchContent).show();
                } else {
                    doctorSearchContent = '<div style="font-size: 14px;padding: 4px 10px;border-bottom: 1px solid #dddddd;">未搜索到患者</div>';
                    $("#"+objContent).html(doctorSearchContent).show();
                }
            },
            error: function (data) {
                alert('没有权限，请联系管理员!');
            }
        });
    }
}
//  前台
function foregroundSearch() {
    var reach = $("#foregroundInputPublic").val().trim();
    if (reach === "" || reach === undefined || reach === null) {
        $("#foregroundSearchContent").hide().html("");
    } else {
        $.ajax({
            url: Feng.ctxPath + "/customer/getCustomerByReach",
            type: "post",
            data: {
                reach: reach,
                pageStart: $("#pageStart").val(),
                pageSize: pageSize
            },
            success: function (info) {
                var data = info.customerInfoList;
                var receptionSearchContent = '';   // 总容器
                var receptionContent = '';     // 内容容器
                var receptionChange = '';      // 变动容器
                if (data.length > 0) {
                    for(var i = 0 ; i < data.length ; i ++ ){
                        //  判断是否欠费
                        if(data[i].accountArrears > 0){
                            //  判断是否会员
                            if(data[i].orthodonticsStatus !== '0.00'){
                                receptionChange = '<div><span>' + data[i].medicalRecordNumber + '</span>&nbsp;/&nbsp;<span>' + data[i].customerPhone2 + '</span>&nbsp;&nbsp;<span class="marketStatus" style="background: red;border-radius: 50%">欠</span>&nbsp;<span style="display: inline-block;"><img src="https://pynnn.oss-cn-hangzhou.aliyuncs.com/ychh001.png" alt="..." width="18px;"></span></div>';
                            }else {
                                receptionChange = '<div><span>' + data[i].medicalRecordNumber + '</span>&nbsp;/&nbsp;<span>' + data[i].customerPhone2 + '</span>&nbsp;&nbsp;<span class="marketStatus" style="background: red;border-radius: 50%">欠</span></div>';
                            }
                        }else {
                            //  判断是否会员
                            if(data[i].orthodonticsStatus !== '0.00'){
                                receptionChange = '<div><span>' + data[i].medicalRecordNumber + '</span>&nbsp;/&nbsp;<span>' + data[i].customerPhone2 + '</span>&nbsp;<span style="display: inline-block;"><img src="https://pynnn.oss-cn-hangzhou.aliyuncs.com/ychh001.png" alt="..." width="18px;"></span></div>';
                            }else {
                                receptionChange = '<div><span>' + data[i].medicalRecordNumber + '</span>&nbsp;/&nbsp;<span>' + data[i].customerPhone2 + '</span></div>';
                            }
                        }
                        //  拼接内容
                        receptionContent += '<div class="marketSearchShop" id="' + data[i].reservationsId + '"><div><img src="' + data[i].customerSexHead + '" alt="" width="50px;"></div><div>' +
                            '<div><span class="marketCustomerName">' + data[i].customerName + '</span>&nbsp;/&nbsp;<span>' + data[i].sex + '</span><span style="margin-left: 10px;">' + data[i].customerAge + '岁</span></div>' +
                            '<div><span>' + data[i].customerPhone + '</span>&nbsp;/&nbsp;<span>' + data[i].phoneSharShip + '</span></div>' + receptionChange + '' +
                            '<div style="position: absolute;right: 10px;top: 50%;transform: translateY(-50%);">' +
                            '<button type="button" class="btn foregroundBtn customerReconsulting">复诊</button>' +
                            '<button type="button" class="btn foregroundBtn insertReDiagnosis">重咨</button></div>' +
                            '</div></div>'
                    }
                    //  整合 html  添加到页面
                    receptionSearchContent = '<div style="font-size: 14px;padding: 4px 10px;border-bottom: 1px solid #dddddd;">找到' + info.count + '个患者</div>' +
                        '<div style="height: 323px;overflow: auto">' + receptionContent + '' +
                        '<div style="text-align: center;padding:5px 10px;border-top: 1px solid #cccccc;font-size: 14px;"><a href="javascript:;" id="foregroundSearchNum">点击加载更多</a></div></div>';
                    $("#foregroundSearchContent").html(receptionSearchContent).show()
                } else {
                    receptionSearchContent = '<div style="font-size: 14px;padding: 4px 10px;border-bottom: 1px solid #dddddd;">未搜索到患者</div>';
                    $("#foregroundSearchContent").html(receptionSearchContent);
                }
            },
            error: function (datas) {
                alert('没有权限，请联系管理员!');
            }
        });
    }
}

$(document).click(function () {
    $("#marketSearchContent,#doctorTcSearchContent,#doctorSearchContent,#doctorTcSearchContents,#foregroundSearchContent").hide();
    $("#pageStart").val("1")
});
//  前台
$("#foregroundInputPublic").click(function (e) {
    e.stopPropagation();
    foregroundSearch();
});
$(document).on('click', '#foregroundSearchNum', function (e) {
    e.stopPropagation();
    $("#pageStart").val(parseInt($("#pageStart").val()) + 1);
    foregroundSearch();
})

//  市场
$("#marketInputPublic").click(function (e) {
    e.stopPropagation();
    marketSearch();
});
$(document).on('click', '#marketSearchNum', function (e) {
    e.stopPropagation();
    $("#pageStart").val(parseInt($("#pageStart").val()) + 1);
    marketSearch();
});

//  TC
$("#doctorTcInputPublic").click(function (e) {
    e.stopPropagation();
    doctorTcSearch('doctorTcInputPublic','doctorTcSearchContent');
});
$(document).on('click', '#doctorTcSearchNum', function (e) {
    e.stopPropagation();
    $("#pageStart").val(parseInt($("#pageStart").val()) + 1);
    doctorTcSearch('doctorTcInputPublic','doctorTcSearchContent');
});

//  结账
$("#doctorTcInputPublics").click(function (e) {
    e.stopPropagation();
    doctorTcSearch('doctorTcInputPublics','doctorTcSearchContents');
});
$(document).on('click', '#doctorTcSearchNums', function (e) {
    e.stopPropagation();
    $("#pageStart").val(parseInt($("#pageStart").val()) + 1);
    doctorTcSearch('doctorTcInputPublics','doctorTcSearchContents');
});

//  医生
$("#doctorInputPublic").click(function (e) {
    e.stopPropagation();
    doctorTcSearch('doctorInputPublic','doctorSearchContent');
});
$(document).on('click', '#doctorSearchNum', function (e) {
    e.stopPropagation();
    $("#pageStart").val(parseInt($("#pageStart").val()) + 1);
    doctorTcSearch('doctorInputPublic','doctorSearchContent');
});